/* 区域及经销商选择相关js */
$(function(){
    var $dealer = $('#popDealerModal'),
        $dealerPop = $('#dealerModal'),
        $area = $('#popAreaModal'),
        $areaPop = $('#areaModal'),
        $areaIput = $('#areaIput'),
        $dealerIput = $('#dealerIput'),
        $areaClear = $('#areaClear'),
        $dealerClear = $('#dealerClear'),
        $areaRadio = $('#areaRadio'),
        $dealerRadio = $('#dealerRadio');

    //区域和经销商是否有值
    if( $.trim( $areaIput.val() ) != '' ) $areaClear.removeClass( 'disabled' );
    if( $.trim( $dealerIput.val() ) != '' ) $dealerClear.removeClass( 'disabled' );

    //通用表格多选效果
    $("table td input[type='checkbox']").click(function(){
        var $span = $(this).parent();
        var $tr = $(this).closest("tr");
        if( $(this).prop("checked")){
            $tr.addClass("active");
            $span.addClass("checked");
        }else{
            $tr.removeClass("active");
            $span.removeClass("checked");
        }
    });
    $("table th input[type='checkbox']").click(function(){
        var $tr = $(this).closest("table").find("tbody tr");
        var $span = $(this).closest("table").find("tr .checker span");
        var $allRadio = $(this).closest("table").find("tbody :checkbox");
        if( $(this).prop("checked") ){
            $tr.addClass("active");
            $span.addClass("checked");
            $allRadio.prop("checked", true);
        }else{
            $tr.removeClass("active");
            $span.removeClass("checked");
            $allRadio.prop("checked", false);
        }
    });
    //选中区域
    $areaRadio.on('click',function(){
        $dealer.addClass('disabled');
        $(this).prop('checked') ? $area.removeClass('disabled') : $area.addClass('disabled');
    });
    //选择区域弹窗
    $area.on("click", function(){
        if( $areaRadio[0] && $areaRadio[0].checked ){
            $areaPop.modal('show');
        }else if( !$areaRadio[0] ){
            $areaPop.modal('show');
        }

    });
    //清空区域
    $areaClear.on('click', function(){
        $(this).addClass('disabled');
        $areaIput.val('');
    });
    //选中经销商
    $dealerRadio.on('click',function(){
        $(this).prop('checked') ? $dealer.removeClass('disabled') : $dealer.addClass('disabled');
    });
    //清空经销商
    $dealerClear.on('click', function(){
        $(this).addClass('disabled');
        $dealerIput.val('');
    });
    //选择所属经销商弹窗
    $dealer.on("click", function(){
        if( $dealerRadio[0] && $dealerRadio[0].checked ){
            $dealerPop.modal('show');
        }else if( !$dealerRadio[0] ){
            $dealerPop.modal('show');
        }
    });

    //经销商选中赋值
    $('#dealerSubmit').on('click', function(){
        var $dealerChecked = $dealerPop.find('table tbody :checkbox:checked'), 
            allVal = '';
        $dealerChecked.each(function( index, item ){
            allVal += ( $dealerChecked.size() == index+1 ) ?  $(item).closest('tr').find('td').eq(1).text() : $(item).closest('tr').find('td').eq(1).text() + ',';
        });
        $dealerIput.val( allVal );
        $dealerPop.modal('hide');
        if( $.trim( $dealerIput.val() ) != '' ) $dealerClear.removeClass( 'disabled' );

    });
    //选择地区弹窗树节点事件
    function getParents( node ){
        var obj = node, parents = [];
        while( obj !== null ){
            parents.push( obj );
            obj = obj.getParentNode();
        };
        return parents.reverse();
    };
    function zTreeOnClick2(event, treeId, treeNode) {
        var parentNames = [];
        $.each( getParents( treeNode ), function( i, n ){
            parentNames.push( n.name );
        });
        $areaIput.val( parentNames.join( '-' ) );
        $areaPop.modal('hide');
        $areaClear.removeClass( 'disabled' );
    };
    //选择经销商弹窗树节点事件(todo)
    function zTreeOnClick(event, treeId, treeNode) {

    };
    var setting = {
        edit: {
            enable: true
        },
        data: {
            simpleData: {
            enable: true
            }
        },
        callback: {
            onClick: zTreeOnClick
        }
    };
    var setting2 = {
        edit: {
            enable: true
        },
        data: {
            simpleData: {
            enable: true
            }
        },
        callback: {
            onClick: zTreeOnClick2
        }
    };
    var zNodes =[
        { id:1, pId:0, name:"上海汽车工业(集团)总公司", open:true},
        { id:11, pId:1, name:"行政大区2区_MG",open:true},
        { id:111, pId:11, name:"雪佛兰",open:true},
        { id:1111, pId:111, name:"经销商礼遇"},
        { id:112, pId:11, name:"凯迪拉克",open:true,isParent:true},
        { id:12, pId:1, name:"行政大区1区_Rw",open:true},
        { id:121, pId:12, name:"制度类"},
        { id:122, pId:12, name:"流程类"},
        { id:123, pId:12, name:"话术类"}
    ];
    $.fn.zTree.init($("#companyTree"), setting, zNodes);
    $.fn.zTree.init($("#companyTree2"), setting2, zNodes);
})